#include "create_group.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>  


int main(int argc, char ** argv){
  CONNECTION_DB conn = init_db(DB_ARQ_CONF);
  PGresult * result = get_group_table(conn);
  int i=0;
  char * outfile;
  if (argc ==2)
    outfile = strdup(argv[1]);
  else
    outfile = strdup(GROUP_ARQ);
  FILE * f = fopen(outfile, "w");
  char * group_name;
  char * group_id;
  for (i=0; i<PQntuples(result); i++){

    group_id = (char *)PQgetvalue(result,i,0);
    group_name = (char * )get_group_name(conn,group_id);

    char string_group[256];
    
    sprintf(string_group,"%s:x:%s:",group_name,group_id);
      
    
    PGresult * result2 = get_users_on_group(conn,group_id);
    int j;
    
    //printf("%s", string_group);
    fprintf(f, "%s", string_group);
    
    for(j=0; j<PQntuples(result2); j++){
      
      char * user_name = get_user_login(conn,(char *)PQgetvalue(result2,j,0));
      
      //printf("%s",user_name);
      fprintf(f,"%s", user_name); 
            
      if(j<PQntuples(result2)-1){
	
	fprintf(f,",");
	//printf(",");
      
      }
      
    }
    fprintf(f,"\n");
    //printf("\n");
  }
  fclose(f);
  free_db(conn);
  return 0;
}

PGresult * get_group_table(CONNECTION_DB conn){
  PGresult * result = NULL;
  
  result = PQexec(conn->pgconn, SELECT_GROUP);
  return result;
}

PGresult * get_users_on_group(CONNECTION_DB conn, char * group_id){
  PGresult * result = NULL;
  char string_conn[255];
  sprintf(string_conn, "%s%s;",SELECT_USERS,group_id);
  result = PQexec(conn->pgconn, string_conn);
  return result;
}

/*---------------------------------------------------------------
retorna NULL, se o usuario nao existe, caso contrario, o login
do usuario
-----------------------------------------------------------------*/
char * get_user_login(CONNECTION_DB conn,  char * id){
  char string_conn[256];
  sprintf(string_conn, "SELECT * FROM GET_USER_LOGIN('%s');",id);
  //char * string_conn = concatplus(3, "SELECT * FROM GET_USER_LOGIN('",
  //			  id,"');");
  PGresult * result = NULL;
  result = PQexec(conn->pgconn, string_conn);
  char * res=NULL;
  if (result!=NULL && PQntuples(result)> 0 && strcmp("", PQgetvalue(result,0,0)) !=0){
    res = (char *)PQgetvalue(result,0,0);
  }
    
  //free(string_conn);
  PQclear(result);
  return res;
}

/*---------------------------------------------------------------
retorna NULL, se o usuario nao existe, caso contrario, o login
do usuario
-----------------------------------------------------------------*/
char * get_group_name(CONNECTION_DB conn,  char * id){
  char string_conn[256];
  sprintf(string_conn, "SELECT * FROM GET_GROUP_NAME('%s');", id);
  
  PGresult * result = NULL;
  result = PQexec(conn->pgconn, string_conn);
  char * res=NULL;
  if (result!=NULL && PQntuples(result)> 0 && strcmp("", PQgetvalue(result,0,0)) !=0){
    res = (char *)PQgetvalue(result,0,0);
  }
    
  
  PQclear(result);
  //printf("login tam %d", strlen(res));
  return res;
}

char * copy(char * name){
  printf("tam %d\n", (int) strlen(name));
  char * str =malloc(sizeof(char)*strlen(name)+1);
  while(*name!='\0'){
    *str++=*name++;
  }
  *str='\0';
  printf("%s %d\n",str, (int) strlen(name));
  return str;
}
